//
//  FeedbackViewController.swift
//  Exam-iOS
//
//  Created by 李胜锋 on 2018/2/6.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import Async

class FeedbackViewController: ViewController, LeftBarButtonItemable {
    
    var textView:LSFTextView!
    
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        setPopRecognizerEnable(false)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.lsf_color16(0xF4F4F4)
        
        title = "意见反馈"
        setLeftItem { [weak self] in
            if String.lsf_hasValue(self?.textView.text) {
                lsf_alert("确认放弃反馈？", cancelTitle: "取消", destructiveTitle: "确认", handle: { (title) in
                    if title == "确认" {
                        self?.navigationController?.popViewController(animated: true)
                    }
                })
            }
            else {
                self?.navigationController?.popViewController(animated: true)
            }
        }

        textView = LSFTextView()
        textView.placeholder = "请输入反馈信息（100字以内）"
        textView.layer.borderColor = UIColor.lightGray.cgColor
        textView.layer.borderWidth = 0.5
        textView.layer.cornerRadius = 4
        textView.clipsToBounds = true
        view.addSubview(textView)
        
        let submitBtn = UIButton.init(type: .custom)
        submitBtn.setTitle("提交反馈", for: .normal)
        submitBtn.setTitleColor(Config.default.themeColor, for: .normal)
        submitBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        submitBtn.clipsToBounds = true
        submitBtn.layer.cornerRadius = 5.0
        submitBtn.layer.borderWidth = 0.5
        submitBtn.layer.borderColor = UIColor.lsf_color16(0xEF838A).cgColor
        view.addSubview(submitBtn)
        
        textView.snp.makeConstraints { (make) in
            make.top.equalTo(topLayoutGuide.snp.bottom).offset(20)
            make.leading.equalTo(10)
            make.trailing.equalTo(-10)
            make.height.equalTo(180)
        }
        
        submitBtn.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize.init(width: 92, height: 32))
            make.top.equalTo(textView.snp.bottom).offset(20)
            make.trailing.equalTo(-10)
        }
        
        
        //提交反馈
        submitBtn.rx.tap.bind { [weak self] in
            guard let strongSelf = self else { return }
            guard let content = strongSelf.textView.text else {
                lsf_alert("反馈内容不能为空")
                return
            }
            
            if content.count <= 0 {
                lsf_alert("反馈内容不能为空")
                return
            }
            
            startAnimating()
            shurenProvider.rx.request(.feedback(content: content)).asObservable().mapRespnseJson().subscribe(onNext: { (data) in
                
            }, onError: { (error) in
                showError(error)
                stopAnimating()
            }, onCompleted: {
                stopAnimating()
                showMessage("反馈成功")
                Async.main(after: 0.25, { [weak self] in
                    self?.navigationController?.popViewController(animated: true)
                })
            }).disposed(by: strongSelf.disposeBag)
            
        }.disposed(by: disposeBag)
    }
}
